package com.sunflower.common.core.event;

import cn.hutool.core.collection.CollectionUtil;
import org.springframework.context.ApplicationEvent;

import java.time.Clock;
import java.util.Collection;

/**
 * @author: SunLuQuan
 * @Date: 2024年03月24日 17:42
 * @Description: BaseEvent
 * TODO
 */
public abstract class BaseEvent<T> extends ApplicationEvent {

    private Sign sign;

    public Sign getSign() {
        return sign;
    }

    @Override
    public T getSource() {
        return (T) source;
    }

    public boolean isCollection() {
        return source instanceof Collection;
    }

    public Collection<T> getSources() {
        if (!isCollection()) {
            return CollectionUtil.toList(getSource());
        }
        return (Collection<T>) source;
    }

    public BaseEvent(T source) {
        this(source, Sign.INSERT);
    }

    public BaseEvent(T source, Sign sign) {
        this(source, Clock.systemDefaultZone(), sign);
    }

    public BaseEvent(Collection<T> sources, Sign sign) {
        this(sources, Clock.systemDefaultZone(), sign);
    }

    public BaseEvent(Collection<T> sources, Clock clock, Sign sign) {
        super(sources, clock);
        this.sign = sign;
    }

    public BaseEvent(T source, Clock clock, Sign sign) {
        super(source, clock);
        this.sign = sign;
    }
}
